<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域-->
      <div class="avatar_box">
        <img src="~assets/img/login.png" alt="">
      </div>
      <!--登录表单区域 用的element-ui-->
      <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登录表单的数据绑定
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      //表单验证
      loginFormRules: {
        //用户名验证  required表示必填
        username:[
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        //密码验证
        password:[
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields() //element-ui表单提供的函数  作用：重置
    },
    login() {
      this.$refs.loginFormRef.validate(async res => {   //element-ui表单提供的函数  作用：预检查
        if(!res) return;
        const reslut = await this.$http.post('login',this.loginForm)  //网络请求
        if(reslut.data.meta.status !== 200) return this.$notify.error('登录失败!')  
        this.$notify.success('登录成功!')   // $notify是element-ui的通知给的全局方法
        //登录成功之后颁发token，将token保存到客户端的sessionStorage中  在F12的application查看
        window.sessionStorage.setItem("token",reslut.data.data.token);
        //跳转页面
        location.hash = "/home";
      })  
    }
  }
}   
</script>

<style scoped>
.login_container{
  width: 100%;
  height: 100%;
  background: linear-gradient(30deg,#33CCFF,#FF66FF);
}

.login_box{
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}

.avatar_box{
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  margin: 0 auto;
  transform: translate(0,-50%);
  background-color: #fff;
  box-shadow: 0 0 10px #eee;
}

.avatar_box img{
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns{
  display: flex;
  justify-content: flex-end;
}
</style>